package com.coollang.smartbra.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.LogUtils;

import de.greenrobot.event.EventBus;

/**
 * @Author xiongxing
 * @Date 2017/3/8
 * @Fuction
 */


public class MyTouchImageView extends ImageView {

    private float mOldX;
    private float mOldY;
    private float mCurrentX;
    private float mCurrentY;
    private int state = -1;

    public static final int normal = 0;
    public static final int left = 1;
    public static final int right = 2;
    public static final int top = 3;
    public static final int bottom = 4;
    public static final int move = 4;


    public MyTouchImageView(Context context) {
        super(context);
    }

    public MyTouchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTouchImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            // 按下
            case MotionEvent.ACTION_DOWN:
                mOldX = event.getX();
                mOldY = event.getY();
                state = normal;
                break;
            // 移动,根据坐标判断移动方向（偏移量是需要定义dimen，这里为了方便）
            case MotionEvent.ACTION_MOVE:
                mCurrentX = event.getX();
                mCurrentY = event.getY();
                if(Math.abs(mCurrentX - mOldX)>30||Math.abs(mCurrentY - mOldY) > 30){
                    state = move;
                }
//                if (mCurrentX - mOldX > 0 && Math.abs(mCurrentY - mOldY) < 10) {
//                    if (Math.abs(mCurrentX - mOldX) > 50)
//                        state = right;
//                } else if (mCurrentX - mOldX < 0
//                        && Math.abs(mCurrentY - mOldY) < 10) {
//                    if (Math.abs(mCurrentX - mOldX) > 50)
//                        state = left;
//                } else if (mCurrentY - mOldY > 0
//                        && Math.abs(mCurrentX - mOldX) < 10) {
//                    if (Math.abs(mCurrentY - mOldY) > 50)
//                        state = bottom;
//                } else if (mCurrentY - mOldY < 0
//                        && Math.abs(mCurrentX - mOldX) < 10) {
//                    if (Math.abs(mCurrentY - mOldY) > 50)
//                        state = top;
//                }
                LogUtils.e("state","state=="+state);
                break;
            // 释放
            case MotionEvent.ACTION_UP:
                switch (state) {
                    case move:
                        EventBus.getDefault().post(new CommonEvent("", CommonEvent.REQUESTSUEECCD, CommonEvent.UNLOCK));
                        LogUtils.e("state","state--"+state);
                        break;
                    case normal:
                        break;
                }
                break;
        }
        //拦截事件
        return true;
    }
}
